package leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2022-01-09 19:21
 **/
public class LeetCode2131 {

    public static int longestPalindrome(String[] words) {
        int len = 0;
        Map<String, Integer> map = new HashMap<>();
        for (String word : words) {
            char c1 = word.charAt(0);
            char c2 = word.charAt(1);
            char[] chars = new char[]{c2, c1};
            String key = String.valueOf(chars);
            if (map.containsKey(key)) {
                len += 4;
                int num = map.get(key);
                if (num == 1) {
                    map.remove(key);
                } else {
                    map.put(key, num - 1);
                }
            } else {
                map.put(word, map.getOrDefault(word, 0) + 1);
            }
        }

        //最后统计一个两个字符相同的字符串
        for (String s : map.keySet()) {
            char c1 = s.charAt(0);
            char c2 = s.charAt(1);
            if (c1 == c2) {
                len += 2;
                break;
            }
        }

        return len;
    }

}
